﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace Clarendon.AzureBilling.Library
{
    /// <summary>
    /// Parses a service response 
    /// </summary>
    public class XmlParseServiceList : IParseResponseXmlServices
    {
        public T ParseXmlString<T>(string xml)
        {
            XDocument document = XDocument.Parse(xml, LoadOptions.None);
            Trace.WriteLine("Parsing XML response and interpreting <HostedService/> nodes");
            var services = (from item in document.Descendants(QueryManager.BuildDefaultNamespaceXmlEntity("HostedService"))
                           select new HostedService()
                           {
                               Name = (string)item.Element(QueryManager.BuildDefaultNamespaceXmlEntity("ServiceName")),
                               Url = (string)item.Element(QueryManager.BuildDefaultNamespaceXmlEntity("Url"))
                           }).ToList();
            Trace.WriteLine("Finished parsing XML response and interpreted <HostedService/> nodes");
            return (T)(object)services;
        }
    }
}
